package com.it.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.it.entity.Category;
import com.it.entity.Product;
import com.it.service.CategoryService;
import com.it.service.ProductService;
import com.it.util.ItdragonUtils;
import com.it.util.Result;
import com.it.util.ResultResponse;
import com.it.util.TableResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：〈药品管理相关接口/控制器〉
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ItdragonUtils itdragonUtils;

    /**
     * 药品管理首页页面跳转
     *
     * @param mv
     * @return
     */
    @RequestMapping("/index.do")
    public ModelAndView index(ModelAndView mv) {
        //查询所有父id为0的分类信息(0即为一级分类)
        List<Map<String, Object>> mapList = categoryService.getCategoryByParentId("0");
        mv.addObject("categoryList", mapList);
        mv.setViewName("/product/index");
        return mv;
    }


    /**
     * 药品管理界面列表异步加载接口
     * 前端通过layui-table插件实现异步加载渲染数据
     *
     * @param product
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @GetMapping("product.do")
    public TableResultResponse reloadTable(int page, int limit, Product product) {
        List<Map<String, Object>> infoList = new ArrayList<>();
        Page<Product> pageInfo = productService.selectPage(product, page, limit);
        for (Product record : pageInfo.getRecords()) {
            Map<String, Object> resultMap = new HashMap<>(16);
            resultMap.put("id", record.getId());
            resultMap.put("name", record.getName());
            Category category = categoryService.getOne(record.getCategoryId());
            resultMap.put("categoryId", categoryService.getOne(category.getParentId()).getName() + "/" + category.getName());
            resultMap.put("uuId", record.getUuId());
            resultMap.put("img", record.getImg());
            resultMap.put("specification", record.getSpecification());
            //价格格式化:保留后两位小数
            DecimalFormat decimalFormat = new DecimalFormat(".00");
            resultMap.put("disPrice", decimalFormat.format(record.getDisPrice()));
            resultMap.put("content", record.getContent());
            infoList.add(resultMap);
        }
        return Result.tableResule(pageInfo.getTotal(), infoList);
    }

    /**
     * 新增药品页面
     * 查询出所有的药品分类,并且封装传回前台
     *
     * @return
     */
    @RequestMapping("/addPage.do")
    public ModelAndView addPage(ModelAndView mv) {
        //查询所有父id为0的分类信息(0即为一级分类)
        List<Map<String, Object>> mapList = categoryService.getCategoryByParentId("0");
        mv.addObject("categoryList", mapList);
        mv.setViewName("/product/addPage");
        return mv;
    }

    /**
     * 新增药品
     *
     * @param product
     * @return
     */
    @PostMapping("/product.do")
    @ResponseBody
    public ResultResponse insert(Product product) {
        boolean result = productService.insert(product);
        if (!result) {
            return Result.resuleError("新增失败");
        }
        return Result.resuleSuccess();
    }

    /**
     * 编辑药品页面弹窗
     *
     * @param id
     * @return
     */
    @RequestMapping("/editPage.do")
    public ModelAndView editPage(String id, ModelAndView mv) {
        Product product = productService.getOne(id);
        mv.addObject("product", product);
        mv.setViewName("/product/editPage");
        return mv;
    }

    /**
     * 编辑药品
     *
     * @param product
     * @return
     */
    @PutMapping("/product.do")
    @ResponseBody
    public ResultResponse edit(Product product) {
        boolean result = productService.editById(product);
        if (!result) {
            return Result.resuleError("编辑失败");
        }
        return Result.resuleSuccess();
    }

    /**
     * 删除药品
     *
     * @param id
     * @return
     */
    @DeleteMapping("/product.do")
    @ResponseBody
    public ResultResponse delete(String id) {
        boolean result = productService.deleteById(id);
        if (!result) {
            return Result.resuleError("删除失败");
        }
        return Result.resuleSuccess();
    }

    /**
     * 获取药品数据总数
     *
     * @param categoryId
     * @return
     */
    @PostMapping("/getCount.do")
    @ResponseBody
    public ResultResponse getCount(String categoryId) {
        Product product = new Product();
        product.setCategoryId(categoryId);
        List<Product> productList = productService.getList(product);
        if (productList.isEmpty()) {
            return Result.resuleSuccess(0);
        }
        return Result.resuleSuccess(productList.size());
    }

    /**
     * 编辑药品页面弹窗
     *
     * @param id
     * @return
     */
    @RequestMapping("/viewPage.do")
    public ModelAndView viewPage(String id, ModelAndView mv) {
        Product product = productService.getOne(id);
        mv.addObject("product", product);
        mv.setViewName("/product/viewPage");
        return mv;
    }
}